package com.example.red_tea_house.moudle.Write.view;

import android.content.Context;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentActivity;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.R;
import com.example.red_tea_house.annotation.ArticleType;
import com.example.red_tea_house.annotation.UserType;
import com.example.red_tea_house.moudle.Write.model.operateStory;
import com.example.red_tea_house.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class  EditActivity extends FragmentActivity {

    private List<String> privacyList;
    private ArrayAdapter<String> privacyListAdapter;

    Spinner privacySpinner=null;
    Button submitArticle;
    EditText articleInput;
    EditText titleInput;
    Toolbar toolbar;

    Context context= MyApplication.getContext();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_layout);

        articleInput = findViewById(R.id.write_edit_text);
        titleInput = findViewById(R.id.write_edit_title);

        toolbar=findViewById(R.id.editLayoutToolbar);
        privacySpinner=findViewById(R.id.edit_spinner);
        submitArticle =findViewById(R.id.story_submit);

        setPrivacyList();
        listenPrivacyList();

        saveArticle();
    }

    private void setPrivacyList(){
        privacyList=new ArrayList<>();
        privacyList.add("私密");privacyList.add("公开");
        privacyListAdapter=new ArrayAdapter<>(this,R.layout.spinner_text,R.id.edit_spinner_item,privacyList);
        privacySpinner.setAdapter(privacyListAdapter);
    }
    private void listenPrivacyList(){
        privacySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
             @Override
             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                 String textSelected=privacyListAdapter.getItem(position);
                 if(textSelected.equals("公开")){
                     toolbar.setBackgroundResource(R.color.white);
                     ToastUtil.showToast(context,"进入公开模式");
                 }else if(textSelected.equals("私密")){
                     toolbar.setBackgroundResource(R.color.teal_200);
                     ToastUtil.showToast(context,"进入私密模式");
                 }
             }

             @Override
             public void onNothingSelected(AdapterView<?> parent) {

             }
        });
    }

    private void saveArticle(){
        submitArticle.setOnClickListener((v)->{
            /*
            * TODO:这里设置的tag都是Article_Remote，会在每次刷新中被删除！
            * */
            if(!articleInput.getText().toString().isEmpty() && !titleInput.getText().toString().isEmpty()){
                operateStory.uploadStory(titleInput.getText().toString(),articleInput.getText().toString(),getCurrentTime(),null,0,ArticleType.Article_Remote, UserType.DefaultAuthor);
                ToastUtil.showToast(context,"文章已经完成");
            }else if(articleInput.getText().toString().isEmpty() && titleInput.getText().toString().isEmpty()){
                operateStory.uploadStory(titleInput.getText().toString(),null,getCurrentTime(),null,0,ArticleType.Article_Remote, UserType.DefaultAuthor);
                ToastUtil.showToast(context,"您还未给故事写具体内容哦！");
            } else if(titleInput.getText().toString().isEmpty()){
                operateStory.uploadStory(null,articleInput.getText().toString(),getCurrentTime(),null,0,ArticleType.Article_Remote, UserType.DefaultAuthor);
                ToastUtil.showToast(context,"您还没有输入标题，上传失败，已保存在草稿");
            }else if(articleInput.getText().toString().isEmpty()) {
                ToastUtil.showToast(context, "您还没有输入内容，上传失败，已保存在草稿");
            }

        });
    }
    private String getCurrentTime(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(System.currentTimeMillis()));
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_HOME){
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(!articleInput.getText().toString().isEmpty()){
                ToastUtil.showToast(context,"文章已保存到草稿");
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
